﻿using System;
using System.Threading.Tasks;

namespace Kimd.Common
{
    public class MonitorHelper
    {
        //系统消息
        private const uint WM_SYSCOMMAND = 0x112;



        //关闭显示器的系统命令  
        private const int SC_MONITORPOWER = 0xF170;



        //2为PowerOff, 1为省电状态，-1为开机
        private const int MonitorPowerOff = 2;



        //广播消息，所有顶级窗体都会接收
        private static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff);



        //导入API函数库,以下用到的函数SendMessage(...)就在user32.dll文件中
        [System.Runtime.InteropServices.DllImport("user32.dll")]



        //此方法的各个参数类型有一些重载，一般还是建议用IntPtr,否则，在64位平台OR其它情况下可能会崩溃
        private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

        [System.Runtime.InteropServices.DllImport("user32 ")]
        public static extern bool LockWorkStation();//这个是调用windows的系统锁定 

        //关闭显示器函数
        public static void CloseMonitor()
        {
            Task.Run(() => {
                //LockWorkStation();
                //SendMessage(this.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, MonitorPowerOff);    //GUI程序用这行
                SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MonitorPowerOff);   //CUI程序用这行 
            });
        }
    }
}